package com.example.jeong.yuejian10.Activity;



import android.app.Activity;
import android.content.Context;

import android.content.SharedPreferences;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.ImageView;
import android.widget.Toast;

import com.example.jeong.yuejian10.Bean.FeedbackBean;
import com.example.jeong.yuejian10.Model.FeedbackModel;
import com.example.jeong.yuejian10.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//发表反馈页
public class AddFeedbackActivity extends Activity {
    //声明
    private ImageView left;
    private Button addfeedback_button;
    private EditText ftitle,finfo;
    private int u_id;
    private String f_title;
    private String f_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_feedback);
        initViews();//初始化
        initEvents();//点击事件处理
    }

    private void initViews() {
        left=(ImageView) findViewById(R.id.left);
        ftitle = (EditText) findViewById(R.id.f_title);
        finfo = (EditText) findViewById(R.id.f_info);
        addfeedback_button = (Button) findViewById(R.id.addfeedback_button);
    }

    private void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        addfeedback_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                u_id = sp.getInt("u_id", 0);
                f_title = ftitle.getText().toString();
                f_info = finfo.getText().toString();
                FeedbackModel feedbackModel = new FeedbackModel();
                //发送请求
                Call<FeedbackBean> call = feedbackModel.addfeedback
                        (u_id, f_title, f_info);
                //2.处理结果
                call.enqueue(new Callback<FeedbackBean>() {
                    @Override
                    public void onResponse(Call<FeedbackBean> call, Response<FeedbackBean> response) {
                        FeedbackBean data = (FeedbackBean) response.body();  //获取数据
                        if (data.getSuccess().equals("1")) {
                            Toast.makeText(AddFeedbackActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                            finish();
                        } else {
                            Toast.makeText(AddFeedbackActivity.this, "添加失败", Toast.LENGTH_LONG).show();

                        }
                    }
                    @Override
                    public void onFailure(Call<FeedbackBean> call, Throwable t) {

                    }
                });
            }

        });
    }
}


